package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.Notice;
import com.chen.dto.R;
import com.chen.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @Author: chen
 * @Date: 2021/4/22 9:15 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "公告管理控制器")
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping
    @ApiOperation(value = "分页查询公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页数"),
            @ApiImplicitParam(name = "size", value = "每页条数")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> list(Page<Notice> page,
                                String title,
                                String startTime,
                                String endTime,
                                String status) {

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        if (StringUtils.isNotBlank(startTime)) {
            queryWrapper.between("created", startTime, endTime);
        }
        if (StringUtils.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }

        queryWrapper.orderByDesc("sort");

        return R.ok(noticeService.page(page, queryWrapper));
    }

    @PostMapping
    @ApiOperation(value = "新增公告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "author", name = "作者"),
            @ApiImplicitParam(value = "content", name = "内容"),
            @ApiImplicitParam(value = "description", name = "描述"),
            @ApiImplicitParam(value = "sort", name = "排序"),
            @ApiImplicitParam(value = "title", name = "标题"),
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public R save(@RequestBody Notice notice) {
        notice.setStatus(1);
        noticeService.save(notice);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "编辑公告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "author", name = "作者"),
            @ApiImplicitParam(value = "content", name = "内容"),
            @ApiImplicitParam(value = "description", name = "描述"),
            @ApiImplicitParam(value = "sort", name = "排序"),
            @ApiImplicitParam(value = "id", name = "公告id"),
            @ApiImplicitParam(value = "title", name = "标题"),
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R update(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return R.ok();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "ids", name = "公告ids"),
    })
    @PreAuthorize("hasAuthority('notice_delete')")
    public R delete(@RequestBody String[] ids) {
        noticeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "启用/禁用公告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "公告id"),
            @ApiImplicitParam(value = "status", name = "状态"),
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R status(@RequestParam("id") String id,
                    @RequestParam("status") String status) {
        noticeService.updateStatus(id, status);
        return R.ok();
    }

    @GetMapping("/simple")
    public R<Page<Notice>> list(Page<Notice> page){
        Page<Notice> list = noticeService.page(page,new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus,1)
                .orderByDesc(Notice::getSort));
        return R.ok(list);
    }

    @GetMapping("/simple/{id}")
    public R<Notice> list(@PathVariable(value = "id",required = false) Long id){
        Notice notice = noticeService.getById(id);
        return R.ok(notice);
    }


}
